package br.com.mware;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TableLayout.LayoutParams;
import android.widget.Button;
import android.widget.TableRow;
import android.widget.TextView;
import br.com.mware.model.Lancamento;

public class MainActivity extends Activity {

	private List<Lancamento> lancamentosTest;
	private int REQUEST_CODE = 1;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        lancamentosTest = new ArrayList<Lancamento>();
        for (int i =0; i < 5; i++) {
        	lancamentosTest.add(new Lancamento(i, new Date(), "Nome"+i, i*10d));
        }

        setContentView(R.layout.main);
        
        preencheTabela();
    }

	private void preencheTabela() {
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		 // Get the TableLayout
        TableLayout tl = (TableLayout) findViewById(R.id.tblLancamentos);
        int current = 0;
        for (Lancamento lancamento : lancamentosTest) {
        	current++;
        	// Create a TableRow and give it an ID
            TableRow tr = new TableRow(this);
            tr.setId(lancamento.getId());
            tr.setLayoutParams( new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.FILL_PARENT));
            
            TextView dataLancamento = new TextView(this);
            dataLancamento.setText(sdf.format(lancamento.getData()));
            dataLancamento.setWidth(80);
            tr.addView(dataLancamento);            
            
            TextView nomeLancamento = new TextView(this);
            nomeLancamento.setText(lancamento.getNome());
            nomeLancamento.setWidth(110);
            tr.addView(nomeLancamento);

            TextView valorLancamento = new TextView(this);
            valorLancamento.setText("R$"+lancamento.getValor());
            valorLancamento.setWidth(50);
            valorLancamento.setGravity(Gravity.RIGHT);
            tr.addView(valorLancamento);

            // Add the TableRow to the TableLayout
            tl.addView(tr, new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.FILL_PARENT));

			
		}
        
        final Button addLancamento = (Button)  findViewById(R.id.btnAddLancamento);
        
        addLancamento.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(v.getContext(), LancamentoActivity.class);
				startActivityForResult(intent, REQUEST_CODE);
			}
		});
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == REQUEST_CODE && resultCode == -1) {
			Log.d("Main", "retornou");
			Log.d("resultCode", String.valueOf(resultCode));
		}
	}
}